﻿using Bank_database_system.业务部;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TreeView;

namespace Bank_database_system
{
    public partial class 操作记录a : UserControl
    {
        public 操作记录a()
        {
            InitializeComponent();
            DateTime today = DateTime.Now;
            DateTime firstDayOfMonth = new DateTime(today.Year, today.Month, 1);
            dateTimePicker1.Value = firstDayOfMonth;
            DateTime lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1);
            dateTimePicker2.Value = lastDayOfMonth;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button8_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            comboBox1.Text = null;
            dataGridView1.DataSource = null;
            DateTime today = DateTime.Now;
            DateTime firstDayOfMonth = new DateTime(today.Year, today.Month, 1);
            dateTimePicker1.Value = firstDayOfMonth;
            DateTime lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1);
            dateTimePicker2.Value = lastDayOfMonth;
            button9_Click(sender, e);
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void 操作记录a_Load(object sender, EventArgs e)
        {
            button9_Click(this, EventArgs.Empty);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void AddColumn(string columnName, string headerText, string dataPropertyName)
        {
            DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn
            {
                Name = columnName,
                HeaderText = headerText,
                DataPropertyName = dataPropertyName,
                DefaultCellStyle = new DataGridViewCellStyle
                {
                    Alignment = DataGridViewContentAlignment.MiddleCenter
                },
                HeaderCell = { Style = { Alignment = DataGridViewContentAlignment.MiddleCenter } }
            };
            dataGridView1.Columns.Add(column);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            // 获取输入值
            string staffId = string.IsNullOrEmpty(textBox1.Text) ? null : textBox1.Text;
            string operationKind = comboBox1.SelectedItem != null ? comboBox1.SelectedItem.ToString() : null;
            DateTime? startTime = dateTimePicker1.Value != null ? (DateTime?)dateTimePicker1.Value : null;
            DateTime? endTime = dateTimePicker2.Value != null ? (DateTime?)dateTimePicker2.Value : null;
            int page = Convert.ToInt32(label8.Text);

            try
            {
                int maxPage = FunctionsforITDepartment.GetOperateRecordCount(staffId, operationKind, startTime, endTime, 20);
                label2.Text = Convert.ToString(maxPage > 1 ? maxPage : 1);

                DataTable staffData = FunctionsforITDepartment.GetOperateRecords(staffId, operationKind, startTime, endTime, page, 20);

                dataGridView1.RowHeadersVisible = false;
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.AllowUserToAddRows = false;
                dataGridView1.Columns.Clear();
                dataGridView1.DoubleBuffered(true);

                // 使用通用方法添加列
                AddColumn("OPERATE_ID", "操作ID", "OPERATE_ID");
                AddColumn("STAFF_ID", "员工ID", "STAFF_ID");
                AddColumn("OPERATE_KIND", "操作类型", "OPERATE_KIND");
                AddColumn("OPERATE_TIME", "操作时间", "OPERATE_TIME");
                AddColumn("OPERATE_IP", "操作IP", "OPERATE_IP");
                AddColumn("OPERATE_CONTENT", "操作内容", "OPERATE_CONTENT");

                dataGridView1.DataSource = staffData;

                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            }
            catch (Exception ex)
            {
                // 错误处理
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button12_Click(object sender, EventArgs e)
        {
            updateMaxPage();
            int page = Convert.ToInt32(label8.Text);
            int maxPage = Convert.ToInt32(label2.Text);
            page--;
            if (page < 1)
            {
                page = 1;
            }
            else if(page>maxPage)
            {
                page=maxPage;
            }
            label8.Text = Convert.ToString(page);
            button9_Click(this, EventArgs.Empty);
        }

        private void button13_Click(object sender, EventArgs e)
        {
            updateMaxPage();
            int page = Convert.ToInt32(label8.Text);
            int maxPage = Convert.ToInt32(label2.Text);
            page++;
            if (page < 1)
            {
                page = 1;
            }
            else if (page > maxPage)
            {
                page = maxPage;
            }
            label8.Text = Convert.ToString(page);
            button9_Click(this, EventArgs.Empty);
        }

        private void updateMaxPage()
        {
            // 获取输入值
            string staffId = string.IsNullOrEmpty(textBox1.Text) ? null : textBox1.Text;
            string operationKind = comboBox1.SelectedItem != null ? comboBox1.SelectedItem.ToString() : null;
            DateTime? startTime = dateTimePicker1.Value != null ? (DateTime?)dateTimePicker1.Value : null;
            DateTime? endTime = dateTimePicker2.Value != null ? (DateTime?)dateTimePicker2.Value : null;
            try
            {
                int maxPage = FunctionsforITDepartment.GetOperateRecordCount(staffId, operationKind, startTime, endTime, 20);
                label2.Text = Convert.ToString(maxPage > 1 ? maxPage : 1);
            }
            catch (Exception ex)
            {
                // 错误处理
            }
        }
    }
    public static class DataGridViewExtensions
    {
        public static void DoubleBuffered(this DataGridView dgv, bool setting)
        {
            Type dgvType = dgv.GetType();
            PropertyInfo pi = dgvType.GetProperty("DoubleBuffered", BindingFlags.NonPublic | BindingFlags.Instance);
            pi.SetValue(dgv, setting, null);
        }
    }


}
